/**
 * @file nd03_comm.c
 * @author tongsheng.tang
 * @brief ND03 communication functions
 * @version 1.x.x
 * @date 2021-11
 * 
 * @copyright Copyright (c) 2021, Shenzhen Nephotonics Inc.
 * 
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//#include "delay.h"
//#include "air32f10x.h"
#include "main.h"




/** iic单字节写函数指针 */
int32_t(*I2C_WriteNBytesLib)(uint8_t, uint16_t, uint8_t *, uint8_t);
/** iic单字节读函数指针 */
int32_t(*I2C_ReadNBytesLib)(uint8_t, uint16_t,uint8_t *, uint8_t);


void ND03_RegisteredIICApi(
        int32_t(*I2C_WriteNBytesFunc)(uint8_t, uint16_t, uint8_t *, uint8_t),
        int32_t(*I2C_ReadNBytesFunc)(uint8_t, uint16_t, uint8_t *, uint8_t)
    )
{
    I2C_ReadNBytesLib = I2C_ReadNBytesFunc;
    I2C_WriteNBytesLib = I2C_WriteNBytesFunc;
}


int32_t ND03_WriteByte(ND03_Dev_t *pNxDevice, uint16_t addr, uint8_t wdata)
{
    int32_t     ret = ND03_ERROR_NONE;

    ret = I2C_WriteNBytesLib(pNxDevice->i2c_dev_addr, addr, &wdata, 1);

	return ret;
}


int32_t ND03_WriteWord(ND03_Dev_t *pNxDevice, uint16_t addr, uint32_t wdata)
{
    int32_t     ret = ND03_ERROR_NONE;
    uint8_t     data[4];

    data[0] = (wdata >> 0) & 0xff;
    data[1] = (wdata >> 8) & 0xff;
    data[2] = (wdata >>16) & 0xff;
    data[3] = (wdata >>24) & 0xff;
    ret = I2C_WriteNBytesLib(pNxDevice->i2c_dev_addr, addr, data, 4);

	return ret;
}


int32_t ND03_ReadWord(ND03_Dev_t *pNxDevice, uint16_t addr, uint32_t *rdata)
{
    int32_t     ret = ND03_ERROR_NONE;
    uint8_t     data[4];
	data[1] = 0xaa;

    ret = I2C_ReadNBytesLib(pNxDevice->i2c_dev_addr, addr, &data[0], 4);
    *rdata = ((uint32_t)data[3]<<24) | ((uint32_t)data[2]<<16) | ((uint32_t)data[1]<<8) | (uint32_t)data[0];

	return ret;
}


int32_t ND03_WriteNWords(ND03_Dev_t *pNxDevice, uint16_t addr, uint32_t *wdata, uint32_t len)
{
    int32_t     ret = ND03_ERROR_NONE;
    uint16_t    i;

    ret |= I2C_WriteNBytesLib(pNxDevice->i2c_dev_addr, addr, (uint8_t*)wdata, len*4);

	return ret;
}


int32_t ND03_ReadNWords(ND03_Dev_t *pNxDevice, uint16_t addr, uint32_t *rdata, uint32_t len)
{
    int32_t     ret = ND03_ERROR_NONE;
    uint16_t    i;

    ret |= I2C_ReadNBytesLib(pNxDevice->i2c_dev_addr, addr, (uint8_t*)rdata, len*4);

	return ret;
}


int32_t ND03_CheckHostEndian(void)
{
    int32_t     ret = ND03_ERROR_NONE;
    uint16_t    u16_data = 0x1234;
    uint8_t     *ptr = (uint8_t *)&u16_data;

    if(ptr[0] == 0x34 && ptr[1] == 0x12) //小端模式
    {
        if(ND03_HOST_ENDIAN != ND03_LITTLE_ENDIAN)
        {
            ret = ND03_ERROR_HOST_ENDIAN;
            NX_PRINTF("**ERROR!!!** Host endian setting error, please check <nd03_def.h> ND03_HOST_ENDIAN define\r\n");
        }
        NX_PRINTF("Host Little-Endian\r\n");
    }
    else if(ptr[1] == 0x34 && ptr[0] == 0x12) //大端模式
    {
        if(ND03_HOST_ENDIAN != ND03_BIG_ENDIAN)
        {
            ret = ND03_ERROR_HOST_ENDIAN;
            NX_PRINTF("**ERROR!!!** Host endian setting error, please check <nd03_def.h> ND03_HOST_ENDIAN define\r\n");
        }
        NX_PRINTF("Host Big-Endian\r\n");
    }
    else //出错
    {
        NX_PRINTF("**ERROR!!!** ND03_CheckHostEndian error\r\n");
        ret = ND03_ERROR_HOST_ENDIAN;
    }

    return ret;
}


